/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eit.eitsite.modules.sys.entity.Dict;
import com.zhuoyue.elmt.data.ResultData;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.PlaCommonQuestion;
import com.zhuoyue.elmt.service.PlaCommonQuestionService;

import java.util.ArrayList;
import java.util.List;

/**
 * 常见问题Controller
 * @author JiangYH
 * @version 2016-12-30
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/plaCommonQuestion")
public class PlaCommonQuestionController extends BaseController {

	@Autowired
	private PlaCommonQuestionService plaCommonQuestionService;
	
	@ModelAttribute
	public PlaCommonQuestion get(@RequestParam(required=false) String id) {
		PlaCommonQuestion entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = plaCommonQuestionService.get(id);
		}
		if (entity == null){
			entity = new PlaCommonQuestion();
		}
		return entity;
	}
	
	@RequiresPermissions("elmt:plaCommonQuestion:view")
	@RequestMapping(value = {"list", ""})
	public String list(PlaCommonQuestion plaCommonQuestion, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PlaCommonQuestion> page = plaCommonQuestionService.findPage(new Page<PlaCommonQuestion>(request, response), plaCommonQuestion); 
		model.addAttribute("page", page);
		return "zhuoyue/elmt/plaCommonQuestionList";
	}

	@RequiresPermissions("elmt:plaCommonQuestion:view")
	@RequestMapping(value = "form")
	public String form(PlaCommonQuestion plaCommonQuestion, Model model) {
		model.addAttribute("plaCommonQuestion", plaCommonQuestion);
		return "zhuoyue/elmt/plaCommonQuestionForm";
	}

	@RequiresPermissions("elmt:plaCommonQuestion:edit")
	@RequestMapping(value = "save")
	public String save(PlaCommonQuestion plaCommonQuestion, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, plaCommonQuestion)){
			return form(plaCommonQuestion, model);
		}
		plaCommonQuestionService.save(plaCommonQuestion);
		addMessage(redirectAttributes, "保存常见问题成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/plaCommonQuestion/?repage";
	}
	
	@RequiresPermissions("elmt:plaCommonQuestion:edit")
	@RequestMapping(value = "delete")
	public String delete(PlaCommonQuestion plaCommonQuestion, RedirectAttributes redirectAttributes) {
		plaCommonQuestionService.delete(plaCommonQuestion);
		addMessage(redirectAttributes, "删除常见问题成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/plaCommonQuestion/?repage";
	}

	@RequestMapping(value = {"mobilelist"})
	public String mobilelist(PlaCommonQuestion plaCommonQuestion, HttpServletRequest request, HttpServletResponse response, Model model) {
		ResultData<List<PlaCommonQuestion>> resultData = new ResultData();
		try {
			System.out.println("查询常见问题");
			plaCommonQuestion.setIsshow("1");
			List<PlaCommonQuestion> list = plaCommonQuestionService.findList(plaCommonQuestion);

			if (null == list) {
				list = new ArrayList<PlaCommonQuestion>();
			}
			resultData.setList(list);
			resultData.setCode("SUCC");
			resultData.setMessage("成功");
		} catch (Exception e) {
			resultData.setCode("FAIL");
			resultData.setMessage("常见问题查询出现异常！");
			e.printStackTrace();
		}
		return super.renderString(response, resultData);
	}

}